$(function() {
	FastClick.attach(document.body);
});

//切换tab样式
$(".tabBtn").each(function(i) {
	$(".tabBtn").eq(i).click(function() {
		$(".trans").addClass("hidden")
		$(".tabBtn").removeClass("active")
		$(this).children(".trans").removeClass("hidden")
		$(this).addClass("active")
	})
})

$(".tabBtn").eq(0).click(function() {
	$(".statistics-total-weekSearch").removeClass("hidden")
	$(".statistics-total-calendar").addClass("hidden")
})

$(".tabBtn").eq(1).click(function() {
	$(".statistics-total-weekSearch").addClass("hidden")
	$(".statistics-total-calendar").removeClass("hidden")
})

var statisticsTotalVue = new Vue({
	el: "body",
	data: {
		currentWeek: "",
		weekStart: "", //周查询 保存当前周范围的第一天
		weekEnd: "", //周查询 保存当前周范围的最后一天
		selectWeek: "", //其他 保存点中该日期所在周
		selectWeekRange: [], //其他 保存点中该日期所在周范围
		isSelect: false
	},
	methods: {
		projectDetail: function() {
			location.href = "statisticsProject.html"
		},
		mapDetail: function() {
			location.href = "statisticsMap.html"
		},
		lineDetail: function() {
			location.href = "statisticsLine.html"
		},
		fileDetail: function() {
			location.href = "statisticsFile.html"
		},
		bookDetail: function() {
			location.href = "statisticsBook.html"
		}
	},
	created: function() {
		_this = this;
		var time = new Date();
		var currentYear = time.getFullYear();
		var currentMouth = time.getMonth() + 1;
		var currentDay = time.getDate();
		var currentWeek = getWeekNumber(currentYear, currentMouth, currentDay)

		var start = getMondayDate()
		var end = getSundayDate()

		_this.weekStart = dateToString(start)
		_this.weekEnd = dateToString(end)
		_this.currentWeek = currentWeek

		$("#date").calendar({
			onDayClick: function(p, dayContainer, year, month, day) {
				var selectYear = parseInt(year);
				var selectMonth = parseInt(month) + 1;
				var selectDay = parseInt(day);
				_this.selectWeek = getWeekNumber(selectYear, selectMonth, selectDay);
				_this.selectWeekRange = getWeekRange(selectYear, _this.selectWeek - 1);
			}
		});

		$("#date").click(function() {
			if(_this.isSelect) {

			} else {
				_this.selectWeek = currentWeek;
				_this.selectWeekRange = getWeekRange(currentYear, _this.selectWeek - 1)
				_this.isSelect = true
			}

		})
	}
})

//判断是否是闰年
function isLeapYear(year) {
	return(year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}

//判断每个月的天数
function getMonthDays(year, month) {
	return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28);
}

//判断所选日期是第几周
function getWeekNumber(y, m, d) {
	var now = new Date(y, m - 1, d),
		year = now.getFullYear(),
		month = now.getMonth(),
		days = now.getDate();
	for(var i = 0; i < month; i++) {
		days += getMonthDays(year, i);
	}
	var yearFirstDay = new Date(year, 0, 1).getDay() || 7;
	var week = null;
	if(yearFirstDay == 1) {
		week = Math.ceil(days / yearFirstDay);
	} else {
		days -= (7 - yearFirstDay + 1);
		week = Math.ceil(days / 7) + 1;
	}
	return week;
}

// 获取当前日期所在周周一的日期对象
function getMondayDate() {
	var time = new Date();
	time.setDate(time.getDate() - time.getDay()+1 );
	return time
}

// 获取当前日期所在周周日的日期对象
function getSundayDate() {
	var time = new Date();
	time.setDate(time.getDate() + 7 - time.getDay());
	return time
}

//对返回出来的日期对象进行处理
function dateToString(date) {
	var year = date.getFullYear()
	var mouth = (date.getMonth() + 1 > 9) ? date.getMonth() + 1 : "0" + (date.getMonth() + 1);
	var day = (date.getDate() > 9) ? date.getDate() : "0" + date.getDate();
	var result = year + "/" + mouth + "/" + day
	return result
}

//得到所在周的具体日期
function getWeekRange(year, index) {
	var begin = new Date(year, 0, 1);
	while(begin.getDay() != 1) {
		begin.setDate(begin.getDate() + 1);
	}
	var end = new Date(year + 1, 0, 1);
	var i = 1;
	var arr = [];
	for(var from = begin; from < end;) {
		if(i == index) {
			arr.push(dateToString(from));
		};
		var j = 6;
		while(j > 0) {
			from.setDate(from.getDate() + 1);
			if(i == index) {
				arr.push(dateToString(from));
			}
			j--;
		};
		if(i == index) {
			return arr;
		};
		from.setDate(from.getDate() + 1);
		i++;
	}
}